package com.itheima.web.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取修改用户id
        String id = request.getParameter("id");

        //2. 调用service查询
        UserService service = new UserServiceImpl();
        User user = service.findById(id);

        //3. 存入请求域中
        request.setAttribute("user",user);

        //4. 转发到update.jsp回显用户信息
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) {
        try {

            //2. 获取所有请求参数
            Map<String, String[]> map = request.getParameterMap();

            //3. 封装到User对象
            User user = new User();
            BeanUtils.populate(user,map);

            //4. 调用service更新
            UserService service = new UserServiceImpl();
            service.updateUser(user);

            //5. 再次查询
            response.sendRedirect(request.getContextPath()+"/userServlet?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1. 获取删除用户id
        String id = request.getParameter("id");

        //2. 调用service删除
        UserService service = new UserServiceImpl();
        service.deleteById(id);

        //3. 重定向到userServlet
        response.sendRedirect(request.getContextPath()+"/userServlet?method=findAll");
    }

    public void add(HttpServletRequest request, HttpServletResponse response) {
        try {

            Map<String, String[]> map = request.getParameterMap();

            User user = new User();
            BeanUtils.populate(user,map);

            UserService service = new UserServiceImpl();
            service.addUser(user);

            response.sendRedirect(request.getContextPath()+"/userServlet?method=findAll");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 调用service查询
        UserService service = new UserServiceImpl();
        List<User> users = service.findAll();

        // 将集合存储到请求域中
        request.setAttribute("users",users);

        // 请求转发到list.jsp页面中显示
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
}
